package com.its.oa.system.permissionRole.dao.impl;

import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.transform.Transformers;

import com.its.oa.dao.base.ItsHibernateDaoSupport;
import com.its.oa.model.PermissionRole;
import com.its.oa.system.permission.vo.PermissionVo;
import com.its.oa.system.permissionRole.dao.PermissionRoleDao;
import com.its.oa.util.EntityValidata;

public class PermissionRoleDaoImpl extends ItsHibernateDaoSupport implements
		PermissionRoleDao {

	@SuppressWarnings("unchecked")
	public List<PermissionRole> findPmsRoleByRoleidPmsid(String roleids, String pmsid) {
		String hql ="from PermissionRole pr where pr.role.id in("+roleids+") and pr.permission.id="+pmsid;
		List<PermissionRole> list = getHibernateTemplate().find(hql);
		if(EntityValidata.isNotEmpty(list)){
			return list;
		}else{
			return null;
		}
	}

	@SuppressWarnings("unchecked")
	public List<PermissionRole> findPmsRoleByRoleid(int roleid) {
		return getHibernateTemplate().find("from PermissionRole where role.id="+roleid);
	}

	@SuppressWarnings("unchecked")
	public List<PermissionVo> findAllPmsRoleVo() {
		Session session = super.getSession();
		String sql = "select distinct p.id pmsid,p.code pmscode,p.name pmsname,p.url url,coalesce(pr.role_id ,0) roleid,pr.btnids btnCtrl,m.name modulename " +
				"from Permission p left join Permission_Role pr on p.id=pr.pms_id " +
				"inner join module m on m.id=p.moduleid order by m.id";
		List<PermissionVo> list = null;
		try {
			list = session.createSQLQuery(sql)
				.setResultTransformer(Transformers.aliasToBean(PermissionVo.class)).list();
		} catch (HibernateException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	public void addPmsRole(PermissionRole pmsRole) {
		getHibernateTemplate().save(pmsRole);		
	}

	@SuppressWarnings("unchecked")
	public List<PermissionVo> findCheckedPmsRoleVo(int roleid) {
		Session session = super.getSession();
		String sql = "select p.id pmsid,p.code pmscode,p.name pmsname,p.url url,pr.role_id roleid,pr.btnids btnCtrl,m.name modulename " +
				"from Permission p,Permission_Role pr,Module m where p.id=pr.pms_id and pr.role_id="+roleid+" "+
				"and m.id=p.moduleid order by m.id";
		List<PermissionVo> list = session.createSQLQuery(sql)
				.setResultTransformer(Transformers.aliasToBean(PermissionVo.class)).list();
		return list;
	}

	@SuppressWarnings("unchecked")
	public List<PermissionVo> findUncheckedPmsRoleVo(String pmsids) {
		Session session = super.getSession();
		String sql = "select distinct p.id pmsid,p.code pmscode,p.url url,p.name pmsname,m.name modulename from Module m,Permission p " +
				"where p.id  not in("+pmsids+") and p.moduleid=m.id";
		List<PermissionVo> list = session .createSQLQuery(sql)
				.setResultTransformer(Transformers.aliasToBean(PermissionVo.class)).list();
		return list;
	}

	public void delPmsRole(PermissionRole pmsRole) {
		getHibernateTemplate().delete(pmsRole);		
	}

	public void updatePmsRole(PermissionRole pmsRole) {
		getHibernateTemplate().update(pmsRole);		
	}

	@SuppressWarnings("unchecked")
	public PermissionRole findPmsRoleByRoleidPmsid(int roleid, int pmsid) {
		List<PermissionRole> list = getHibernateTemplate().find("from PermissionRole pr where pr.role.id="+roleid+" and pr.permission.id="+pmsid);
		if(EntityValidata.isNotEmpty(list)){
			return list.get(0);
		}
		return null;
	}
	
}
